
class DoubanSearch extends AbstractPlugin {

  async fetchSearchResults(query) {
    const searchUrl = `https://www.douban.com/search?cat=1002&q=${encodeURIComponent(query)}`;
    const result  = await doHttpRequestSync(searchUrl, {});
    if(result.status !== 'success') {
      return null;
    }
    const data = result.body;

    const parser = new DOMParser();
    const doc = parser.parseFromString(data, 'text/html');

    const searchResults = [];

    const results = doc.querySelectorAll('.result');

    results.forEach(result => {
      const searchResult = new SearchResult();
      searchResult.site_icon = '/plugins/douban/icon.png';
      searchResult.site_name = 'douban.com';

      const imageElement = result.querySelector(".pic a img");
//      searchResult.image = imageElement ? imageElement.src.replace('s_ratio_poster', 'm') : "";
      searchResult.image = imageElement ? imageElement.src : "";

      const titleElement = result.querySelector('.title a');
      const title = titleElement ? titleElement.textContent.trim() : '';
      const typeElement = result.querySelector('.title h3 span');
      const type = typeElement ? typeElement.textContent.trim() : '';
      searchResult.title = `${type} ${title}`;

      const href = titleElement ? titleElement.getAttribute('href') : '';
      const urlParam = new URLSearchParams(new URL(href).search).get('url');
      searchResult.link = decodeURIComponent(urlParam);

      const originalNameElement = result.querySelector('.subject-cast');
      const originalName = originalNameElement ? originalNameElement.textContent.trim() : '';
      const descriptionElement = result.querySelector('p');
      const description = descriptionElement ? descriptionElement.textContent.trim() : '';
      searchResult.description = `${originalName}\n${description}`;

      searchResults.push(searchResult);
    });
    return searchResults;
  }

}
// Registry.registerClass("DoubanSearch", new DoubanSearch());